#include "AcronymManager.h"

AcronymManager::AcronymManager(AVDoc avDoc, AcronymDictionary& acronymDictionary, SelectionExplorer* pSelectionExplorer):
	TagManager(avDoc, _T("Span"), true, acronymDictionary, true, pSelectionExplorer)
{
}

AcronymManager::~AcronymManager(void)
{
}

// Return whether first element is greater than the second
bool UDgreater ( TagRegion &elem1, TagRegion &elem2 )
{
	return elem1.Offset <= elem2.Offset;
}



void AcronymManager::FillTagRegions(PDEText pdeText, vector<TagRegion>& tagRegions)
{
	tagRegions.clear();

	CString str;

	StringFromPDEText(str, pdeText);

	for(unsigned int a=0; a < _acronymDictionary.Items.size(); ++a)
	{
		int offset;
		int length = _acronymDictionary.Items[a]->Key.GetLength();

		int searchOffset = 0;

		while((offset = str.Find(_acronymDictionary.Items[a]->Key, searchOffset)) >= 0)
		{	
			ASFixedQuad asFixedQuad1;
			ASFixedQuad asFixedQuad2;
			PDETextGetQuad (pdeText, kPDETextChar, offset, &asFixedQuad1 );
			PDETextGetQuad (pdeText, kPDETextChar, offset + length - 1, &asFixedQuad2 );


			TagRegion tagRegion;

			tagRegion.Offset = offset;
			tagRegion.Length = length;
			tagRegion.AcronymIndex = a;
			tagRegion.rect.top = max(asFixedQuad1.tl.v, asFixedQuad2.tl.v);
			tagRegion.rect.bottom = max(asFixedQuad1.bl.v, asFixedQuad2.bl.v);
			tagRegion.rect.left  = min(asFixedQuad1.tl.h, asFixedQuad2.tl.h);
			tagRegion.rect.right  = max(asFixedQuad1.tr.h, asFixedQuad2.tr.h);

			searchOffset = offset + length;

			tagRegions.push_back(tagRegion);
		}
	}

	std::sort(tagRegions.begin(), tagRegions.end(), UDgreater);	
}